var interval;
var timeout;
var progress = 0;

var date = {
	"year" : 1983,
	"month" : 08,
	"day" : 01
};

var singleItemArray = [
		"single 1", "single 2", "single 3"
];

var singleSelectedIndex = 0;

var multiItemArray = [
		{
			"multi1" : false
		}, {
			"multi2" : false
		}, {
			"multi3" : true
		}
];

function init() {
	_initHeader();
	
	var array = new Array();
	
	array.push("showProgressDialog");
	array.push("showProgressBarDialog");
	array.push("showDatePicker");
	array.push("showDatePickerWithData");
	array.push("showTimePicker");
	array.push("showTimePickerWithData");
	array.push("showContextMenu");
	array.push("showSingleSelect");
	array.push("showMultiSelect");
	array.push("setOptionMenu");
	
	draw_list(array);
}

function setOnListClickHandler(idx) {
	switch(idx) {
		case 0 :
			testShowProgressDialog();
			
		break;
		
		case 1 :
			testShowProgressBarDialog();
			
		break;
		
		case 2 :
			testShowDatePicker();
			
		break;
		
		case 3 :
			testShowDatePickerWithData();
			
		break;
		
		case 4 :
			testShowTimePicker();
			
		break;
		
		case 5 :
			testShowTimePickerWithData();
			
		break;
		
		case 6 :
			testShowContextMenu();
			
		break;
		
		case 7 :
			testShowSingleSelect();
			
		break;
		
		case 8 :
			testShowMultiSelect();
			
		break;
		
		case 9 :
			testSetOptionMenu();
			
		break;
		
		default :

		break;
	}
}

/*
 * testShowProgressDialog
 */
function testShowProgressDialog() {
	var option = {
		"message" : "please wait...",
		"cancelCallback" : "showProgressDialogCancelCallback",
		"cancelable" : true,
		"color" : "grey",
		"useCustomUI" : true
	};
	platformUIComponent.showProgressDialog(option);
	this.timeout = setTimeout("dismissProgressDialog()", 3000);
}

function dismissProgressDialog() {
	platformUIComponent.dismissProgressDialog();
}

function showProgressDialogCancelCallback() {
	
	alert("showProgressDialog가 cancel되었습니다.");
}

/*
 * showProgressBarDialog
 */
function testShowProgressBarDialog() {
	var option = {
		"message" : "please wait..",
		"cancelable" : true,
		"cancelCallback" : "showProgressBarDialogCancelCallback",
	};
	
	platformUIComponent.showProgressBarDialog(option);
	this.interval = setInterval("setProgress()", 25);
}

function setProgress() {
	if(this.progress != 100) {
		this.progress = this.progress + 1;
		platformUIComponent.setProgress(this.progress);
	} else if(this.progress == 100) {
		clearInterval(this.interval);
		platformUIComponent.dismissProgressBarDialog();
		this.progress = 0;
	}
}

function showProgressBarDialogCancelCallback() {
	clearInterval(this.interval);
	progress = 0;
	alert("showProgressBarDialog가 cancel되었습니다.");
}

/*
 * showDatePicker
 */
function testShowDatePicker() {
	var dateOption = {
		"x" : window.event.x,
		"y" : window.event.y
	};
	
	platformUIComponent.showDatePicker(showDatePickerCallback, dateOption);
}

function showDatePickerCallback(date) {
	alert(date.year + " . " + date.month + " . " + date.day);
}

/*
 * showDatePickerWithData
 */
function testShowDatePickerWithData() {
	var dateOption = {
		"x" : window.event.x,
		"y" : window.event.y
	};
	
	platformUIComponent.showDatePickerWithData(date, showDatePickerCallback, dateOption);
}

/*
 * showTimePicker
 */
function testShowTimePicker() {
	var timeOption = {
		"x" : window.event.x,
		"y" : window.event.y,
		"is24HourView" : false
	};
	
	platformUIComponent.showTimePicker(showTimePickerCallback, timeOption);
}

function showTimePickerCallback(time) {
	alert(time.hour + " : " + time.minute + " : " + time.ampm);
}

/*
 * showTimePickerWithData
 */
function testShowTimePickerWithData() {
	var time = {
		"hour" : 7,
		"minute" : 10,
		"ampm" : "pm"
	};
	
	var option = {
		"x" : window.event.x,
		"y" : window.event.y,
		"is24HourView" : false
	};
	
	platformUIComponent.showTimePickerWithData(time, showTimePickerCallback, option);
}

/*
 * showContextMenu
 */
function testShowContextMenu() {
	var menuItems = {
		"items" : [
				{
					"item 1" : "item1Callback"
				}, {
					"item 2" : "item2Callback"
				}
		]
	};
	
	var option = {
		"title" : "This is title of context menu"
	};
	
	platformUIComponent.showContextMenu(menuItems, option);
}

function item1Callback() {
	alert("item 1 is selected.");
}

function item2Callback() {
	alert("item 2 is selected.");
}

/*
 * showSingleSelect
 */
function testShowSingleSelect() {
	var selection = {
		"items" : singleItemArray,
		"defValue" : singleSelectedIndex,
		"title" : "showSingleSelect title",
		"x" : window.event.x,
		"y" : window.event.y
	};
	
	selection.callback = "singleSelectCallback";
	platformUIComponent.showSingleSelect(selection);
}

function singleSelectCallback(selectedIndex) {
	alert("Selected  item : " + singleItemArray[selectedIndex]);
	singleSelectedIndex = selectedIndex;
}

/*
 * showMultiSelect
 */
function testShowMultiSelect() {
	var selection = {
		"items" : multiItemArray,
		"callback" : "multiSelectCallback",
		"title" : "showMultiSelect title",
		"x" : window.event.x,
		"y" : window.event.y
	};
	
	selection.callback = "multiSelectCallback";
	platformUIComponent.showMultiSelect(selection);
}

function multiSelectCallback(selectedItems) {
	var result = "Selected  item : ";
	var selectedItemIndex = new Array();
	var j = 0;
	
	for( var i = 0; i < selectedItems.length; i++) {
		if(selectedItems[i] == 1) {
			selectedItemIndex[j] = i;
			j++;
		}
	}
	
	for( var i = 0; i < selectedItemIndex.length; i++) {
		for( var name in multiItemArray[selectedItemIndex[i]])
			result = result + name;
		
		if(i != selectedItemIndex.length - 1)
			result = result + ", ";
	}
	
	alert(result);
	
	for( var item in this.multiItemArray) {
		for(key in this.multiItemArray[item]) {
			this.multiItemArray[item][key] = selectedItems[item];
		}
	}
}

function testSetOptionMenu() {
	var menuItems = [
			{
				"title" : "선택",
				"callback" : "selectCallback",
				"icon" : ""
			}, {
				"title" : "추가",
				"callback" : "addCallback",
				"icon" : ""
			}, {
				"title" : "종료",
				"callback" : "exitCallabck"
			}
	];
	
	platformUIComponent.setOptionMenu(menuItems);
}

function selectCallback() {
	alert("selectCallback");
}

function addCallback() {
	alert("addCallback");
}

function exitCallabck() {
	alert("exitCallback");
}